package cn.momoshenchi.product.service.impl;

import cn.momoshenchi.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.momoshenchi.common.utils.PageUtils;
import cn.momoshenchi.common.utils.Query;

import cn.momoshenchi.product.dao.CategoryDao;
import cn.momoshenchi.product.entity.CategoryEntity;
import cn.momoshenchi.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService
{
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
//    @Autowired
//    private  CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params)
    {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public Long[] findCategoryPath(Long id)
    {

        List<Long> path = new LinkedList<>();
        findParent(id, path);
        Collections.reverse(path);
        return (Long[]) path.toArray(new Long[0]);
    }

    @Override
    @Transactional
    public void updateCascade(CategoryEntity category)
    {
        this.updateById(category);
        if (!StringUtils.isBlank(category.getName()))
        {
            categoryBrandRelationService.updateCategory(category.getCatId(), category.getName());
        }
    }

    private void findParent(Long id, List<Long> path)
    {
        path.add(id);
        CategoryEntity categoryEntity = this.getById(id);
        if (categoryEntity.getParentCid() != 0)
        {
            findParent(categoryEntity.getParentCid(), path);
        }
    }


    @Override
    public List<CategoryEntity> listWithTree()
    {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        CategoryEntity categoryEntity=new CategoryEntity();
        categoryEntity.setCatId(0L);
        categoryEntity.setCatLevel(-1);
        return  getChildren2(entities,categoryEntity);
    }

    private List<CategoryEntity> getChildren2(List<CategoryEntity> all, CategoryEntity categoryEntity)
    {

        return all.stream()
                .filter(item -> categoryEntity.getCatId().equals(item.getParentCid()))
                .peek(item -> item.setChildren(getChildren2(all, item)))
                .sorted((a, b) -> {
                    return a.getSort()-b.getSort();
                })
                .collect(Collectors.toList());
    }
}